package com.rectcircle.scalatrateset.controller

class MainServlet extends ScalatratestStack {
	
	get("/") {
		<html>
			<body>
				<h1>Hello, world!</h1>
				Say
				<a href="hello-scalate">hello to Scalate</a>
				.
				中文测试
			</body>
			
		</html>
	}
	
	get("/hello/:name") {
		<h1>
			{params("name")}
		</h1>
	}
	
	get("/say/*/to/*") {
		// Matches "GET /say/hello/to/world"
		val splat = multiParams("splat")
		<h1>
			say {splat(0)} to {splat(1)}
		</h1>
	}
	
	get("/download/*.*") {
		// Matches "GET /download/path/to/file.xml"
		multiParams("splat") // == Seq("path/to/file", "xml")
		val splat = multiParams("splat")
		<h1>
			you download is {splat(0)}.{splat(1)}
		</h1>
	}
	
	get("""^\/f(.*)/b(.*)""".r) {
		// Matches "GET /foo/bar"
		multiParams("captures") // == Seq("oo", "ar")
	}
	
	get("/foo", request.getRemoteHost == "127.0.0.1") {
		// Overrides "GET /foo" for local users
		<h1>from host 127.0.0.1</h1>
	}
	
	get("/articles/:id") {
		
		// /articles/52?foo=uno&bar=dos&baz=three&foo=anotherfoo
		
		params("id") // => "52"
		params("foo") // => "uno" (discarding the second "foo" parameter value)
//		params("unknown") // => generates a NoSuchElementException
		params.get("unknown") // => None - this is what Scala does with unknown keys in a Map
		
		multiParams("id") // => Seq("52")
		multiParams("foo") // => Seq("uno", "anotherfoo")
		multiParams("unknown") // => an empty Seq
		
		<pre>
			params("id") {params("id")}
			params("foo") {params("foo")}
			params("unknown") 这会抛出异常
			params.get("unknown") {params.get("unknown")}
			params.get("id") {params.get("id")}
			
			multiParams("id") {multiParams("id")}
			multiParams("foo") {multiParams("foo")}
			multiParams("unknown") {multiParams("unknown") }
		</pre>
		
	}
	
	get("/articles-by/:author/:page") {
		val author:String = params.getOrElse("author", halt(400))
		//val author:String = params.getOrElse("author", "unknown")
		val page:Int = params.getOrElse("page", "1").toInt
		// now do stuff with your params
		
		<h1> author:{author} page:{page} </h1>
	}
	
	post("/json"){
		<h1> {request.body} </h1>
	}
	
	before() {
		println("请求处理前")
	}
	
	after(){
		println("请求处理之后")
	}
	
	get("/"){
		redirect("/someplace/else")
	}

	get("/"){
		halt(status = 403,
			reason = "Forbidden",
			headers = Map("X-Your-Mother-Was-A" -> "hamster",
				"X-And-Your-Father-Smelt-Of" -> "Elderberries"),
			body = <h1>Go away or I shall taunt you a second time!中文测试</h1>)
	}
	
	get("/guess/*") {
		<h1>"You missed!"{params("who")}</h1>
	}
	
	get("/guess/:who") {
		params("who") match {
			case "Frank" => "You got me!"
			case _ => pass()
		}
	}
	
	get("/guess/?") {
		<h1>You missed!中文测试</h1>
	}
	
//	notFound {
//		<h1>Not found. Bummer.</h1>
//	}
}
